package com.fernfx.xingtan.my.ui;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.common.entity.user.UserInfoEntity;
import com.fernfx.xingtan.user.AccountManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by admin on 2018/8/15.
 * 账户安全
 */

public class AccountSafeActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.account_text_tv)
    TextView accountTextTv;
    @BindView(R.id.phone_number_tv)
    TextView phoneNumberTv;

    public static final int UPDATE_PNUMBER_STATUS = 1;
    //user name key
    public static final String UPDATE_USER_NAME_KEY = "user_name";

    public static void start(Context context) {
        Intent intent = new Intent(context, AccountSafeActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_my_account_safe;
    }

    @Override
    protected void initView() {
        setToolbarTitle("账户安全");
    }

    @Override
    protected void initData() {
        UserInfoEntity userInfoEntity = AccountManager.getUserInfo().getObj();
        //设置账号
        accountTextTv.setText(String.valueOf(userInfoEntity.getUserId()));
        //设置手机号
        phoneNumberTv.setText(formatMoblie(userInfoEntity.getMobile()));
    }

    @OnClick({R.id.change_pnumber_rlt, R.id.update_pwd_rlt, R.id.real_verify_rlt})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.change_pnumber_rlt:
                UpdatePNumberActivity.start(this);
                break;
            case R.id.update_pwd_rlt:
                UpdatePwdActivity.start(mContext);
                break;
            case R.id.real_verify_rlt:
                RealVerifyActivity.start(mContext);
                break;
        }
    }

    //格式化手机号
    private String formatMoblie(String moblie) {
        if (TextUtils.isEmpty(moblie)) {
            return "";
        }

        final StringBuilder moblieSbuilder = new StringBuilder();
        //array items
        StringBuilder sb  =new StringBuilder();
        for (int i = 0; i < moblie.length(); i++) {
            char c = moblie.charAt(i);
            if (i >= 3 && i <= 6) {
                sb.append('*');
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }

        if (requestCode == UPDATE_PNUMBER_STATUS) {
            String mobliePhone = data.getStringExtra(UPDATE_USER_NAME_KEY);
            phoneNumberTv.setText(mobliePhone);
            //更新本地的缓存手机号
            AccountManager.updateMobilePhone(mobliePhone);
        }
    }
}
